<!DOCTYPE html>
<html lang="en">





<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="My Personal Website For Blog">
  <meta name="author" content="黄宇辉">
  <meta name="keywords" content="learning note,hexo blog">
  <title>Spring:Bean - 欢迎参观小灰灰的网站哟 ヾ(◍°∇°◍)ﾉﾞ ~</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/vs2015.min.css" />
  

  


<!-- 主题依赖的图标库，不要自行修改 -->
<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_yg9cfy8wd6.css">

<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_pjno9b9zyxs.css">

<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>ANONYMOUS</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                Home
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                Archives
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                Tags
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                About
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="view intro-2" id="background" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container text-center white-text fadeInUp">
            <span class="h2" id="subtitle">
              
            </span>

            
              
  <div class="mt-3 post-meta">
    <i class="iconfont icon-date-fill" aria-hidden="true"></i>
    <time datetime="2019-05-14 22:04">
      May 14, 2019 pm
    </time>
  </div>


<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.6k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      41
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <div class="post-content mx-auto" id="post">
            
            <article class="markdown-body">
              <h2 id="学习笔记-Spring-Bean"><a href="#学习笔记-Spring-Bean" class="headerlink" title="学习笔记 : Spring Bean"></a>学习笔记 : Spring Bean</h2><h3 id="Bean-的配置"><a href="#Bean-的配置" class="headerlink" title="Bean 的配置"></a>Bean 的配置</h3><p><em>简介 : Spring可以被看成一个大型工厂,其作用是<code>生产</code>和<code>管理</code>Spring容器中的<code>Bean</code>,前提是需要在Sring配置文件中进行配置. Spring容器支持<code>XML</code>和<code>Properties</code>两种格式的配置文件.其前者最为常用哟! 在配置文件中,通常为一个Bean配置<code>id</code>或<code>name</code>和<code>class</code>两个属性即可,注意的是如果Bean中未指定id和name,则Spring会将class值当做id使用.</em></p>
<h3 id="Bean-的实例化"><a href="#Bean-的实例化" class="headerlink" title="Bean 的实例化"></a>Bean 的实例化</h3><p><em>简介 : 在Spring中,想要使用容器中的Bean时需要先将其实例化,其实例化的方式有三种,如下所示.</em></p>
<ol>
<li>构造器实例化(<code>最常用</code>) : Spring容器通过Bean对应类中默认的无参构造方法来实例化Bean.</li>
<li>静态工厂方式实例化 : 将Bean配置中的class属性指定静态工厂类,然后使用<code>factory-method</code>属性来指定所创建的静态工厂方法.</li>
<li>实例工厂方式实例化 : 将需要实例化的Bean通过<code>factory-bean</code>属性指向配置的实例工厂,然后使用<code>factory-method</code>属性确定使用工厂中的具体方法.</li>
</ol>
<h5 id="Bean-的实例化案例"><a href="#Bean-的实例化案例" class="headerlink" title="Bean 的实例化案例"></a>Bean 的实例化案例</h5><p><em>通过一个示例程序来演示实例化Bean的三种方式.</em></p>
<ol>
<li><p><em>Bean类</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.spring.bean.instance;

<span class="hljs-comment">//构造器实例化</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Bean1</span> </span>&#123;

&#125;

<span class="hljs-keyword">package</span> pers.huangyuhui.spring.bean.instance;

<span class="hljs-comment">//静态工厂方式实例化  </span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Bean2</span> </span>&#123;

&#125;

<span class="hljs-keyword">package</span> pers.huangyuhui.spring.bean.instance;

<span class="hljs-comment">//实例工厂方式实例化</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Bean3</span> </span>&#123;

&#125;</code></pre></div>
</li>
<li><p><em>MyBean2Factory.java : 静态工厂</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.spring.bean.factory;

<span class="hljs-keyword">import</span> pers.huangyuhui.spring.bean.instance.Bean2;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@ClassName</span>: MyBean2Factory
 * <span class="hljs-doctag">@Description</span>: 静态工厂方式实例化
 * <span class="hljs-doctag">@author</span>: HuangYuhui
 * <span class="hljs-doctag">@date</span>: May 24, 2019 2:08:00 PM
 *
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyBean2Factory</span> </span>&#123;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Bean2 <span class="hljs-title">createBean</span><span class="hljs-params">()</span> </span>&#123;
		<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Bean2();
	&#125;
&#125;</code></pre></div>
</li>
<li><p><em>MyBean3Factory.java : 实例工厂</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.spring.bean.factory;

<span class="hljs-keyword">import</span> pers.huangyuhui.spring.bean.instance.Bean3;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@ClassName</span>: MyBean3Factory
 * <span class="hljs-doctag">@Description</span>: 实例工厂方式实例化
 * <span class="hljs-doctag">@author</span>: HuangYuhui
 * <span class="hljs-doctag">@date</span>: May 24, 2019 2:16:03 PM
 *
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyBean3Factory</span> </span>&#123;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">MyBean3Factory</span><span class="hljs-params">()</span> </span>&#123;
        System.out.println(<span class="hljs-string">"this is constructor ~"</span>);
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> Bean3 <span class="hljs-title">createBean</span><span class="hljs-params">()</span> </span>&#123;
		<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Bean3();
	&#125;
&#125;</code></pre></div>
</li>
<li><p><em>applicationContext.xml : Spring核心配置文件</em></p>
<div class="hljs"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans"</span>
	<span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">"http://www.w3.org/2001/XMLSchema-instance"</span>
	<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"</span>&gt;</span>
	
	<span class="hljs-comment">&lt;!-- 构造器实例化 --&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"bean1"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"pers.huangyuhui.spring.bean.instance.Bean1"</span>/&gt;</span>

	<span class="hljs-comment">&lt;!-- 静态工厂方式实例化 --&gt;</span>
	<span class="hljs-comment">&lt;!-- factory-method: 指定工厂方法 --&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"bean2"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"pers.huangyuhui.spring.bean.factory.MyBean2Factory"</span> <span class="hljs-attr">factory-method</span>=<span class="hljs-string">"createBean"</span>/&gt;</span>

	<span class="hljs-comment">&lt;!-- 实例工厂方式实例化 --&gt;</span>
	<span class="hljs-comment">&lt;!-- factory-bean:指定配置的实例工厂 --&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"myBean3Factory"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"pers.huangyuhui.spring.bean.factory.MyBean3Factory"</span>/&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"bean3"</span> <span class="hljs-attr">factory-bean</span>=<span class="hljs-string">"myBean3Factory"</span> <span class="hljs-attr">factory-method</span>=<span class="hljs-string">"createBean"</span>/&gt;</span>
	
<span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span></code></pre></div>
</li>
<li><p><em>InstanceTest.java : 测试类</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.spring.bean.instance.test;

<span class="hljs-keyword">import</span> org.junit.BeforeClass;
<span class="hljs-keyword">import</span> org.junit.Ignore;
<span class="hljs-keyword">import</span> org.junit.Test;
<span class="hljs-keyword">import</span> org.springframework.context.ApplicationContext;
<span class="hljs-keyword">import</span> org.springframework.context.support.ClassPathXmlApplicationContext;

<span class="hljs-keyword">import</span> pers.huangyuhui.spring.bean.instance.Bean1;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@ClassName</span>: InstanceTest
 * <span class="hljs-doctag">@Description</span>: 测试
 * <span class="hljs-doctag">@author</span>: HuangYuhui
 * <span class="hljs-doctag">@date</span>: May 24, 2019 1:57:41 PM
 *
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InstanceTest</span> </span>&#123;

	<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> ApplicationContext applicationContext;

	<span class="hljs-meta">@BeforeClass</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">()</span> </span>&#123;
		applicationContext = <span class="hljs-keyword">new</span> ClassPathXmlApplicationContext(<span class="hljs-string">"applicationContext.xml"</span>);
	&#125;

	<span class="hljs-meta">@Test</span>
	<span class="hljs-meta">@Ignore</span>
	<span class="hljs-comment">// 构造器实例化</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">instanceTest1</span><span class="hljs-params">()</span> </span>&#123;
		Bean1 bean1 = applicationContext.getBean(Bean1<span class="hljs-class">.<span class="hljs-keyword">class</span>)</span>;

		<span class="hljs-comment">// result : pers.huangyuhui.spring.bean.instance.Bean1@59402b8f</span>
		System.out.println(bean1);
	&#125;

	<span class="hljs-meta">@Test</span>
	<span class="hljs-meta">@Ignore</span>
	<span class="hljs-comment">// 静态工厂方式实例化</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">instanceTest2</span><span class="hljs-params">()</span> </span>&#123;

		<span class="hljs-comment">// result : pers.huangyuhui.spring.bean.instance.Bean2@70ed52de</span>
		System.out.println(applicationContext.getBean(<span class="hljs-string">"bean2"</span>));
	&#125;

	<span class="hljs-meta">@Test</span>
	<span class="hljs-meta">@Ignore</span>
	<span class="hljs-comment">// 实例工厂方式实例化</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">instanceTest3</span><span class="hljs-params">()</span> </span>&#123;

		<span class="hljs-comment">// result :</span>
		<span class="hljs-comment">// this is constructor ~</span>
		<span class="hljs-comment">// pers.huangyuhui.spring.bean.instance.Bean3@70ed52de</span>
		System.out.println(applicationContext.getBean(<span class="hljs-string">"bean3"</span>));
	&#125;

&#125;</code></pre></div>



</li>
</ol>
<h3 id="Bean-的作用域"><a href="#Bean-的作用域" class="headerlink" title="Bean 的作用域"></a>Bean 的作用域</h3><p><em>简介 : 在Spring 4.3中为Bean定义了七种作用域,如下所示.</em></p>
<ol>
<li>Singleton(<code>单例</code>) : 使用Singleton定义的Bean在Spring容器中将只有一个实例,也就是说,无论有多少个Bean引用它,始终指向同一个对象,其也是Spring默认的作用域.</li>
<li>prototype(<code>原型</code>) : 每次通过Spring容器获取prototype定义的Bean时,容器都将创建一个新的Bean实例.</li>
<li>request : ..</li>
<li>session : ..</li>
<li>globalSession : ..</li>
<li>application : ..</li>
<li>websocket : ..</li>
</ol>
<h4 id="Bean-的作用域案例"><a href="#Bean-的作用域案例" class="headerlink" title="Bean 的作用域案例"></a>Bean 的作用域案例</h4><p><em>下面通过简单程序来分别演示<code>singleton</code>和<code>prototype</code>作用域</em></p>
<ol>
<li><p><em>Bean类</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.spring.bean.scope;

<span class="hljs-comment">//Bean的作用域: singleton </span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Bean4</span> </span>&#123;
&#125;


<span class="hljs-keyword">package</span> pers.huangyuhui.spring.bean.scope;

<span class="hljs-comment">//Bean的作用域: prototype</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Bean5</span> </span>&#123;
&#125;</code></pre></div>
</li>
<li><p><em>Spring核心配置文件</em></p>
<div class="hljs"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans"</span>
	<span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">"http://www.w3.org/2001/XMLSchema-instance"</span>
	<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"</span>&gt;</span>

	<span class="hljs-comment">&lt;!-- singleton(default) --&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"bean4"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"pers.huangyuhui.spring.bean.scope.Bean4"</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">"singleton"</span>/&gt;</span>
	<span class="hljs-comment">&lt;!-- prototype --&gt;</span>
	<span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"bean5"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"pers.huangyuhui.spring.bean.scope.Bean5"</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">"prototype"</span>/&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span></code></pre></div>
</li>
<li><p><em>测试类</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.spring.bean.scope.test;

<span class="hljs-keyword">import</span> org.junit.BeforeClass;
<span class="hljs-keyword">import</span> org.junit.Ignore;
<span class="hljs-keyword">import</span> org.junit.Test;
<span class="hljs-keyword">import</span> org.springframework.context.ApplicationContext;
<span class="hljs-keyword">import</span> org.springframework.context.support.ClassPathXmlApplicationContext;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@ClassName</span>: ScopeTest
 * <span class="hljs-doctag">@Description</span>: 测试
 * <span class="hljs-doctag">@author</span>: HuangYuhui
 * <span class="hljs-doctag">@date</span>: May 24, 2019 2:53:10 PM
 *
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ScopeTest</span> </span>&#123;

	<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> ApplicationContext applicationContext;

	<span class="hljs-meta">@BeforeClass</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">()</span> </span>&#123;
		applicationContext = <span class="hljs-keyword">new</span> ClassPathXmlApplicationContext(<span class="hljs-string">"applicationContext.xml"</span>);
	&#125;

	<span class="hljs-meta">@Test</span>
	<span class="hljs-meta">@Ignore</span>
	<span class="hljs-comment">// Bean的作用域: singleton</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">singletonTest</span><span class="hljs-params">()</span> </span>&#123;
		<span class="hljs-comment">// result:</span>
		<span class="hljs-comment">// pers.huangyuhui.spring.bean.scope.Bean4@7c137fd5</span>
		<span class="hljs-comment">// pers.huangyuhui.spring.bean.scope.Bean4@7c137fd5</span>
		System.out.println(applicationContext.getBean(<span class="hljs-string">"bean4"</span>));
		System.out.println(applicationContext.getBean(<span class="hljs-string">"bean4"</span>));
	&#125;

	<span class="hljs-meta">@Test</span>
	<span class="hljs-meta">@Ignore</span>
	<span class="hljs-comment">// Bean的作用域: prototype</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">prototypeTest</span><span class="hljs-params">()</span> </span>&#123;
		<span class="hljs-comment">// result: pers.huangyuhui.spring.bean.scope.Bean5@3e08ff24</span>
		System.out.println(applicationContext.getBean(<span class="hljs-string">"bean5"</span>));
	&#125;

&#125;</code></pre></div>



</li>
</ol>
<h3 id="Bean-的装配方式"><a href="#Bean-的装配方式" class="headerlink" title="Bean 的装配方式"></a>Bean 的装配方式</h3><p><em>简介 : Bean的装配方式可以理解为依赖注入,Bean的装配方式既Bean依赖注入的方式. Spring容器支持多种形式的Bean的装配方式,如基于<code>XML</code>的装配,基于<code>Annotation</code>的装配等..</em></p>
<h4 id="基于XML的装配"><a href="#基于XML的装配" class="headerlink" title="基于XML的装配"></a>基于XML的装配</h4><p><em>Spring提供了两种基于XML的装配方式: 设值注入(<code>Setter Injection</code>)和构造注入(<code>Constructor Injection</code>). 在Spring实例化Bean的过程中,<code>Spring首先会调用Bean的默认构造方法来实例化Bean对象,然后通过反射的方式调用setter方式来注入属性值</code>.因此,设值注入要求一个Bean必须满足一下两点要求.</em></p>
<ol>
<li>Bean类必须提供一个默认的无参构造方法.</li>
<li>Bean类必须为需要注入的属性提供对应的<code>setter</code>方法.</li>
</ol>
<p><em>下面通过一个简单示例程序来展示基于XML的Bean的装配方式.</em></p>
<ol>
<li><p><em>User.java : Java bean</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.spring.assemble.bean;

<span class="hljs-keyword">import</span> java.util.List;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@ClassName</span>: User
 * <span class="hljs-doctag">@Description</span>: 用户信息
 * <span class="hljs-doctag">@author</span>: HuangYuhui
 * <span class="hljs-doctag">@date</span>: May 24, 2019 3:37:58 PM
 *
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> </span>&#123;

	<span class="hljs-keyword">private</span> String name;
	<span class="hljs-keyword">private</span> List&lt;String&gt; phone;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">User</span><span class="hljs-params">()</span> </span>&#123;

	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">User</span><span class="hljs-params">(String name, List&lt;String&gt; phone)</span> </span>&#123;
		<span class="hljs-keyword">super</span>();
		<span class="hljs-keyword">this</span>.name = name;
		<span class="hljs-keyword">this</span>.phone = phone;
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span><span class="hljs-params">()</span> </span>&#123;
		<span class="hljs-keyword">return</span> name;
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span><span class="hljs-params">(String name)</span> </span>&#123;
		<span class="hljs-keyword">this</span>.name = name;
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> List&lt;String&gt; <span class="hljs-title">getPhone</span><span class="hljs-params">()</span> </span>&#123;
		<span class="hljs-keyword">return</span> phone;
	&#125;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setPhone</span><span class="hljs-params">(List&lt;String&gt; phone)</span> </span>&#123;
		<span class="hljs-keyword">this</span>.phone = phone;
	&#125;

	<span class="hljs-meta">@Override</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">toString</span><span class="hljs-params">()</span> </span>&#123;
		<span class="hljs-keyword">return</span> <span class="hljs-string">"User [name="</span> + name + <span class="hljs-string">", phone="</span> + phone + <span class="hljs-string">"]"</span>;
	&#125;

&#125;</code></pre></div>
</li>
<li><p><em>applicationContext.xml : Spring核心配置文件</em></p>
<div class="hljs"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans"</span>
	<span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">"http://www.w3.org/2001/XMLSchema-instance"</span>
	<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"</span>&gt;</span>
	
	 <span class="hljs-comment">&lt;!-- 利用构造注入方式装配User实例 --&gt;</span>
	 <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"user1"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"pers.huangyuhui.spring.assemble.bean.User"</span>&gt;</span>
	 	<span class="hljs-tag">&lt;<span class="hljs-name">constructor-arg</span> <span class="hljs-attr">index</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"YUbuntu0109-1"</span>/&gt;</span>
	 	<span class="hljs-tag">&lt;<span class="hljs-name">constructor-arg</span> <span class="hljs-attr">index</span>=<span class="hljs-string">"1"</span>&gt;</span>
	 		<span class="hljs-tag">&lt;<span class="hljs-name">list</span>&gt;</span>
	 			<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>"15111111111"<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span>
	 			<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>"15211111111<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span>
	 		<span class="hljs-tag">&lt;/<span class="hljs-name">list</span>&gt;</span>
	 	<span class="hljs-tag">&lt;/<span class="hljs-name">constructor-arg</span>&gt;</span>
	 <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>
	 <span class="hljs-comment">&lt;!-- 使用设值注入方式装配User实例 --&gt;</span>
	 <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"user2"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"pers.huangyuhui.spring.assemble.bean.User"</span>&gt;</span>
	 	<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"name"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"YUbuntu0109-2"</span>/&gt;</span>
	 	<span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"phone"</span>&gt;</span>
	 		<span class="hljs-tag">&lt;<span class="hljs-name">list</span>&gt;</span>
	 			<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>"13111111111"<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span>
	 			<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>"18111111111"<span class="hljs-tag">&lt;/<span class="hljs-name">value</span>&gt;</span>
	 		<span class="hljs-tag">&lt;/<span class="hljs-name">list</span>&gt;</span>
	 	<span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span>
	 <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span></code></pre></div>
</li>
<li><p><em>AssembleTest.java : 测试类</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.spring.assemble.test;

<span class="hljs-keyword">import</span> org.junit.BeforeClass;
<span class="hljs-keyword">import</span> org.junit.Ignore;
<span class="hljs-keyword">import</span> org.junit.Test;
<span class="hljs-keyword">import</span> org.springframework.context.ApplicationContext;
<span class="hljs-keyword">import</span> org.springframework.context.support.ClassPathXmlApplicationContext;

<span class="hljs-keyword">import</span> pers.huangyuhui.spring.assemble.controller.UserController;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@ClassName</span>: AssembleTest
 * <span class="hljs-doctag">@Description</span>: 测试
 * <span class="hljs-doctag">@author</span>: HuangYuhui
 * <span class="hljs-doctag">@date</span>: May 24, 2019 3:49:42 PM
 *
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AssembleTest</span> </span>&#123;

	<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> ApplicationContext applicationContext;

	<span class="hljs-meta">@BeforeClass</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">()</span> </span>&#123;
		applicationContext = <span class="hljs-keyword">new</span> ClassPathXmlApplicationContext(<span class="hljs-string">"applicationContext.xml"</span>);
	&#125;

	<span class="hljs-meta">@Test</span>
	<span class="hljs-comment">// @Ignore</span>
	<span class="hljs-comment">// 测试基于XML的Bean配置</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">xmlBeanAssembleTest</span><span class="hljs-params">()</span> </span>&#123;

		<span class="hljs-comment">// result: User [name=YUbuntu0109-1, phone=["15111111111", "15211111111]]</span>
		System.out.println(applicationContext.getBean(<span class="hljs-string">"user1"</span>));
		<span class="hljs-comment">// result: User [name=YUbuntu0109-2, phone=["13111111111", "18111111111"]]</span>
		System.out.println(applicationContext.getBean(<span class="hljs-string">"user2"</span>));
	&#125;

&#125;</code></pre></div>


</li>
</ol>
<h4 id="基于Annotation的装配"><a href="#基于Annotation的装配" class="headerlink" title="基于Annotation的装配"></a>基于Annotation的装配</h4><p><em>简介 : 通过注解(<code>Annotation</code>)来实现Bean的装配工作可以解决XML配置文件过于臃肿的问题,且便于后期维护. 其常用注解如下.</em></p>
<ol>
<li><code>@Repository</code> : 用于将数据访问层(DAO层)的类标识为Spring中的Bean.</li>
<li><code>@Service</code> : 通常作用在业务层(Service层),用于将业务层的类标识为Spring中的Bean.</li>
<li><code>@Controller</code> : 通过作用在控制层(如Spring MVC的Controller),用于将控制层的类标识为Spring中的Bean.</li>
<li><code>@Autowired</code> : 用于对Bean的属性变量,属性的setter方法及构造方法进行标注,配合对用的注解处理器来完成Bean的自动配置工作.</li>
</ol>
<p><em>下面通过一个简单案例来演示如何通过这些注解来装配Bean.</em></p>
<ol>
<li><p><em>UserDao.java</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.spring.assemble.dao;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">UserDao</span> </span>&#123;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">save</span><span class="hljs-params">()</span></span>;
&#125;</code></pre></div>
</li>
<li><p><em>UserDaoImple.java</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.spring.assemble.dao.impl;

<span class="hljs-keyword">import</span> org.springframework.stereotype.Repository;

<span class="hljs-keyword">import</span> pers.huangyuhui.spring.assemble.dao.UserDao;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@ClassName</span>: UserDaoImpl
 * <span class="hljs-doctag">@Description</span>: UserDao的实现类
 * <span class="hljs-doctag">@author</span>: HuangYuhui
 * <span class="hljs-doctag">@date</span>: May 24, 2019 4:13:53 PM
 *
 */</span>
<span class="hljs-meta">@Repository</span>(<span class="hljs-string">"userDao"</span>) <span class="hljs-comment">// 将UserDaoImpl类标识为Spring中的Bean</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserDaoImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">UserDao</span> </span>&#123;

	<span class="hljs-meta">@Override</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">save</span><span class="hljs-params">()</span> </span>&#123;
		System.out.println(<span class="hljs-string">"UserDao : Save function ~"</span>);
	&#125;

&#125;</code></pre></div></li>
</ol>
<ul>
<li><em>其中使用<code>@Respository</code>注解将UserDaoImpl类标识为Spring中的Bean,其写法相当于配置文件中的<code>&lt;bean id=&quot;userDao&quot; class=&quot;pers.huangyuhui.spring.assemble.dao.impl.UserDaoImpl&quot;/&gt;</code></em></li>
</ul>
<ol start="3">
<li><p><em>UserService.java</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.spring.assemble.dao;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">UserService</span> </span>&#123;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">save</span><span class="hljs-params">()</span></span>;
&#125;</code></pre></div>
</li>
<li><p><em>UserServiceImpl.java</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.spring.assemble.dao.impl;

<span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;
<span class="hljs-keyword">import</span> org.springframework.stereotype.Service;

<span class="hljs-keyword">import</span> pers.huangyuhui.spring.assemble.dao.UserDao;
<span class="hljs-keyword">import</span> pers.huangyuhui.spring.assemble.dao.UserService;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@ClassName</span>: UserServiceImpl
 * <span class="hljs-doctag">@Description</span>: UserService的实现类
 * <span class="hljs-doctag">@author</span>: HuangYuhui
 * <span class="hljs-doctag">@date</span>: May 24, 2019 4:20:15 PM
 *
 */</span>
<span class="hljs-meta">@Service</span> <span class="hljs-comment">// 将UserServiceImpl类标识为Spring中的Bean</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserServiceImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">UserService</span> </span>&#123;

	<span class="hljs-meta">@Autowired</span> <span class="hljs-comment">// 自动配置Bean</span>
	<span class="hljs-keyword">private</span> UserDao userDao;

	<span class="hljs-meta">@Override</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">save</span><span class="hljs-params">()</span> </span>&#123;
		userDao.save();
		System.out.println(<span class="hljs-string">"UserService : Save function ~"</span>);
	&#125;

&#125;</code></pre></div>
</li>
<li><p><em>UserController.java</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.spring.assemble.controller;

<span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;
<span class="hljs-keyword">import</span> org.springframework.stereotype.Repository;

<span class="hljs-keyword">import</span> pers.huangyuhui.spring.assemble.dao.UserService;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@ClassName</span>: UserController
 * <span class="hljs-doctag">@Description</span>: TODO
 * <span class="hljs-doctag">@author</span>: HuangYuhui
 * <span class="hljs-doctag">@date</span>: May 24, 2019 4:33:56 PM
 *
 */</span>
<span class="hljs-meta">@Repository</span> <span class="hljs-comment">/// 将UserController类标识为Spring中的Bean</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserController</span> </span>&#123;

	<span class="hljs-meta">@Autowired</span> <span class="hljs-comment">// 自动配置Bean</span>
	<span class="hljs-keyword">private</span> UserService userService;

	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">save</span><span class="hljs-params">()</span> </span>&#123;
		userService.save();
		System.out.println(<span class="hljs-string">"UserController : Save function ~"</span>);
	&#125;

&#125;</code></pre></div>
</li>
<li><p><em>AssembleTest.java : 测试类</em></p>
<div class="hljs"><pre><code class="hljs java"><span class="hljs-keyword">package</span> pers.huangyuhui.spring.assemble.test;

<span class="hljs-keyword">import</span> org.junit.BeforeClass;
<span class="hljs-keyword">import</span> org.junit.Ignore;
<span class="hljs-keyword">import</span> org.junit.Test;
<span class="hljs-keyword">import</span> org.springframework.context.ApplicationContext;
<span class="hljs-keyword">import</span> org.springframework.context.support.ClassPathXmlApplicationContext;

<span class="hljs-keyword">import</span> pers.huangyuhui.spring.assemble.controller.UserController;

<span class="hljs-comment">/**
 * <span class="hljs-doctag">@ClassName</span>: AssembleTest
 * <span class="hljs-doctag">@Description</span>: 测试
 * <span class="hljs-doctag">@author</span>: HuangYuhui
 * <span class="hljs-doctag">@date</span>: May 24, 2019 3:49:42 PM
 *
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AssembleTest</span> </span>&#123;

	<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> ApplicationContext applicationContext;

	<span class="hljs-meta">@BeforeClass</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">()</span> </span>&#123;
		applicationContext = <span class="hljs-keyword">new</span> ClassPathXmlApplicationContext(<span class="hljs-string">"applicationContext.xml"</span>);
	&#125;

	<span class="hljs-meta">@Test</span>
	<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">annotationAssembleTest</span><span class="hljs-params">()</span> </span>&#123;
		UserController userController = (UserController) applicationContext.getBean(<span class="hljs-string">"userController"</span>);
		userController.save();
	&#125;

&#125;</code></pre></div>
</li>
<li><p><em>Spring核心配置文件</em></p>
<div class="hljs"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans"</span>
	<span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">"http://www.w3.org/2001/XMLSchema-instance"</span>
	<span class="hljs-attr">xmlns:context</span>=<span class="hljs-string">"http://www.springframework.org/schema/context"</span>
	<span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"</span>&gt;</span>
	 
	 <span class="hljs-comment">&lt;!-- 扫描指定包下的所有Bean类,进行注解解析 --&gt;</span>
	 <span class="hljs-tag">&lt;<span class="hljs-name">context:component-scan</span> <span class="hljs-attr">base-package</span>=<span class="hljs-string">"pers.huangyuhui.spring.assemble"</span>/&gt;</span>
	 
<span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span></code></pre></div>
</li>
<li><p><em>程序运行结果</em></p>
<div class="hljs"><pre><code class="hljs ada">UserDao : <span class="hljs-type">Save</span> <span class="hljs-keyword">function</span> <span class="hljs-title">~</span>
UserService : <span class="hljs-type">Save</span> <span class="hljs-keyword">function</span> <span class="hljs-title">~</span>
UserController : <span class="hljs-type">Save</span> <span class="hljs-keyword">function</span> <span class="hljs-title">~</span></code></pre></div></li>
</ol>

            </article>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Spring/">Spring</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" target="_blank" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <div class="post-prev col-6">
                    
                    
                      <a href="/2019/05/15/MyBatis%E7%9A%84%E6%A0%B8%E5%BF%83%E9%85%8D%E7%BD%AE/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">MyBatis的核心配置</span>
                        <span class="visible-mobile">Previous</span>
                      </a>
                    
                  </div>
                  <div class="post-next col-6">
                    
                    
                      <a href="/2019/05/12/VOA-Facebook-Co-founder-Calls-for-New-Laws-Limiting-Big-Tech/">
                        <span class="hidden-mobile">VOA:Facebook Co-founder Calls for New Laws Limiting Big Tech</span>
                        <span class="visible-mobile">Next</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </div>
                </div>
              
            </div>

            
          </div>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;TOC</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">Search</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">keyword</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  

  <footer class="mt-5">
  <div class="text-center py-3">
    <div>
      <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a>
      <i class="iconfont icon-love"></i>
      <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener">
        <span>Fluid</span></a>
    </div>
    
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


    

    
  </div>
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: 'article.markdown-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "Spring:Bean&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>







  
  
    <script>
      !function (e, t, a) {
        function r() {
          for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
          requestAnimationFrame(r)
        }

        function n() {
          var t = "function" == typeof e.onclick && e.onclick;
          e.onclick = function (e) {
            t && t(), o(e)
          }
        }

        function o(e) {
          var a = t.createElement("div");
          a.className = "heart", s.push({
            el: a,
            x: e.clientX - 5,
            y: e.clientY - 5,
            scale: 1,
            alpha: 1,
            color: c()
          }), t.body.appendChild(a)
        }

        function i(e) {
          var a = t.createElement("style");
          a.type = "text/css";
          try {
            a.appendChild(t.createTextNode(e))
          } catch (t) {
            a.styleSheet.cssText = e
          }
          t.getElementsByTagName("head")[0].appendChild(a)
        }

        function c() {
          return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
        }

        var s = [];
        e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {
          setTimeout(e, 1e3 / 60)
        }, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()
      }(window, document);
    </script>
  














</body>
</html>
